package hied;

import java.util.Map;

import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;

@Root(name = "Knowledge")
public class Knowledge {
	private static final SimilarityCalculator SIMILARITY_CALCULATOR = new SimilarityCalculator();

	@ElementMap(name = "wordsCorrectForms")
	private Map<String, String> wordsCorrectForms;

	@ElementMap(name = "wordsPositions")
	private Map<String, WordPositions> wordsPositions;

	private String findSimilar(String incorrect) {
		double bestSimilarity = Parameters.getMinSimilarity();
		String mostSimilar = null;
		double similarity;
		for (String word : this.wordsCorrectForms.keySet()) {
			similarity = SIMILARITY_CALCULATOR.match(word, incorrect);
			if (similarity > bestSimilarity) {
				mostSimilar = word;
			}
		}
		return this.wordsCorrectForms.get(mostSimilar);
	}

	public String getCorrectForm(String incorrect) {
		String result = this.wordsCorrectForms.get(incorrect);
		if (result != null) {
			return result;
		}
		return this.findSimilar(incorrect);
	}

	public AvgDev getWordPosition(String word, float wordsCount) {
		return this.wordsPositions.get(word).getBestPosition(wordsCount);
	}

	public void setWordsCorrectForms(Map<String, String> wordsCorrectForms) {
		this.wordsCorrectForms = wordsCorrectForms;
	}

	public void setWordsPositions(Map<String, WordPositions> wordsPositions) {
		this.wordsPositions = wordsPositions;
	}
}
